import { ActivitySession } from './sessions/activity'
import { AudioTrackSession } from './sessions/audio'
import { GenericDialogSession } from './sessions/dialog'
import { MediaPlayerSession } from './sessions/media/player'
import { MediaRecorderSession } from './sessions/media/recorder'
import { TextToSpeechSession } from './sessions/tts'
import { WidgetButtonSession } from './sessions/widgets/button'
import { WidgetCheckBoxSession } from './sessions/widgets/checkBox'
import { WidgetEditorSession } from './sessions/widgets/editor'
import { WidgetFileUploaderSession } from './sessions/widgets/fileUploader'
import { WidgetRadioSession } from './sessions/widgets/radio'
import { WidgetTextSession } from './sessions/widgets/text'
import { addSessionClass, clearSessionClass } from './ref'
import { onActivated, onDeactivated } from 'vue'
import { WidgetSpinnerSession } from './sessions/widgets/spinner'
import { RequestsSession } from './sessions/requests'
import {  WidgetProgressBarSession } from './sessions/widgets/progressBar'
import {  WidgetImageSession } from './sessions/widgets/image'
import { SensorSession } from "./sessions/sensor";
import { FileSession } from './sessions/file'
import { SoundPoolSession } from './sessions/soundPool'

let activity: ActivitySession = null;

export function load(elegantRender: HTMLElement) {
    // 加载所有的会话类
    addSessionClass(ActivitySession)
    addSessionClass(AudioTrackSession)
    addSessionClass(GenericDialogSession)
    addSessionClass(MediaPlayerSession)
    addSessionClass(MediaRecorderSession)
    addSessionClass(TextToSpeechSession)
    addSessionClass(WidgetButtonSession)
    addSessionClass(WidgetCheckBoxSession)
    addSessionClass(WidgetEditorSession)
    addSessionClass(WidgetFileUploaderSession)
    addSessionClass(WidgetImageSession)
    addSessionClass(WidgetProgressBarSession)
    addSessionClass(WidgetRadioSession)
    addSessionClass(WidgetTextSession)
    addSessionClass(WidgetSpinnerSession)
    addSessionClass(RequestsSession)
    addSessionClass(SensorSession)
    addSessionClass(FileSession)
    addSessionClass(SoundPoolSession)
    // 监听激活和取消激活
    onActivated(() => {
        if (activity)
            activity.start()
    })
    onDeactivated(() => {
        if (activity)
            activity.stop()
    })
    function connect() {
        // 实例化activity会话对象
        activity = new ActivitySession(elegantRender)
        // 断线5秒后自动重连
        activity.onDestroyedListener = () => {
            elegantRender.append('正在连接服务器，请稍后...')
            setTimeout(() => connect(), 5000)
        }
    }
    connect()
}
export function unload() {
    // 销毁activity
    if (activity)
        activity.destroy()
    activity = null
    clearSessionClass()
}